#!/bin/bash -ilex
#export FLUTTER_HOME="/Users/kzcai/fvm/versions/3.22.0"
#export PATH="$FLUTTER_HOME/bin:$PATH"

echo "用户根目录->userPath ：$1"
echo "应用简称->shortName ：$2"
echo "分支名称->branch ：$3"
echo "部署环境id->envId ：$4"
echo "主项目名->mainProject ：$5"
echo "工作目录->workspace ：$6"
echo "构建产物上传目录->svnUploadDir ：$7"
echo "mpassId->mpAppId ：$8"
echo "是否定版->isDecided ：$9"
echo "构建次数->env.BUILD_NUMBER ：${10}"
echo "节点名称->theServerName：${11}"
echo "版本->versionNo：${12}"
echo "加固后带绝对路径apk包名->apkSecName：${13}"
echo "定版分发程序包存放目录->apkSecName：${14}"


#===flutter clean===
fvm flutter clean
#===获取本地依赖===
fvm flutter pub get --offline
buildInfo="";
#===拼接命令行===
if [ -n "${10}" ]; then
    buildInfo+=" --build-number=${10}";
fi

if [ -n "${12}" ]; then
    buildInfo+=" --build-name=${12}";
fi

#===执行打包===
fvm flutter build apk $buildInfo

version=$(grep 'version:' pubspec.yaml | sed -e 's/#.*//' -e 's/[[:space:]]*version:[[:space:]]*//' -e 's/[+"].*//' | tr -d "'\"")
echo $version

if test -e build/app/outputs/flutter-apk/app-release.apk; then
#===将文件发送至存放文件夹===

#if [ -n "$7" ]; then
#    #===创建存放文件夹===
#    mkdir -p "$7"
#    scp -r build/app/outputs/flutter-apk/app-release.apk  "$7/$2_v${versionNo}.apk"
#fi
scp -r build/app/outputs/flutter-apk/app-release.apk  "数字康养_v${version}.apk"

#===清理编译文件===
fvm flutter clean
else
  throw_exception() {
    echo "打包发生错误" >&2
    exit 1
  }
fi


